package com.hq.user.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @author Administrator
 */
public class Base64Utils {

	public static final String UTF_8 = "UTF-8";
	public static Base64.Encoder encoder;
	public static Base64.Encoder urlEncoder;
	public static Base64.Decoder decoder;
	public static Base64.Decoder urlDecoder;

	static {
		encoder = Base64.getEncoder();
		urlEncoder = Base64.getUrlEncoder();
		decoder = Base64.getDecoder();
		urlDecoder = Base64.getUrlDecoder();
	}

	//encode
	public static byte[] encode(byte[] bytes) {
		return encoder.encode(bytes);
	}

	public static String encode(String string) {
		byte[] encode = encode(string.getBytes());
		try {
			return new String(encode, UTF_8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
